const mongoose = require('mongoose');

const sectionSchema = new mongoose.Schema({
  // projectId 字段已移除，支持多对多关系
  name: {
    type: String,
    required: true,
    trim: true,
    maxlength: 200
  },
  order: {
    type: Number,
    required: false  // 改为可选，因为顺序在项目中管理
  },
  description: {
    type: String,
    trim: true,
    maxlength: 1000
  },
  forms: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Form'
  }],
  isTemplate: {
    type: Boolean,
    default: true  // 默认为模板章节
  },
  config: {
    showInToc: {
      type: Boolean,
      default: true
    },
    pageBreak: {
      type: Boolean,
      default: false
    },
    // 新增：章节标题（访视名称）对齐方式，默认居中
    titleAlign: {
      type: String,
      enum: ['left', 'center'],
      default: 'center'
    },
    numbering: {
      enabled: {
        type: Boolean,
        default: true
      },
      format: {
        type: String,
        default: '1.'
      }
    }
  }
}, {
  timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' }
});

// 索引
sectionSchema.index({ isTemplate: 1 });
sectionSchema.index({ name: 1 });
sectionSchema.index({ createTime: -1 });

module.exports = mongoose.model('Section', sectionSchema);